-- TiedPig
-- Created by chengb Feb/3/2016
-- 被捆绑的野猪格子

require "game/ui/form/dungeon/UIDungeonTiedPig"

-- 初始化
function initTiedPig(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到SM野猪建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 不在副本中，不处理
            if not DungeonInstanceM.isInInstance() then
                return;
            end

            -- 音效
            AudioM.playFx("pig_damage");

            if not DungeonM.isAllMonsterDead() then
                -- 如果在副本中，需要击杀所有怪物方可拾取
                self:openBuildingView(nil, -20, buildingInfo["dialog2"], getLocStr("btn_text_leave"));
                return;
            end

            -- 怪物全部清除完了
            UIMgr.getCurrentScene():removeFormByName("UIDungeonTiedPig");
            local uiDungeonTiedPig = UIDungeonTiedPig.create(self.gridData:getPos());
            UIMgr.getCurrentScene():addForm(uiDungeonTiedPig);
        end
    end

    -- 播放烤猪动画
    -- 火燃烧动画
    local fireAnimation = createEffectAnim(1189);
    fireAnimation:play(0, 19, true);
    self.item:addChild(fireAnimation);

    -- 烤猪哭泣
    local pigAnimation = createEffectAnim(1187);
    pigAnimation:play(0, 23, true);
    self.item:addChild(pigAnimation);

    -- 用一张透明的图片来接收点击事件
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.icon:setOpacity(0);
end

